/**
 * @param {character[][]} grid
 * @param {string} target
 * @return {boolean}
 */
var wordPuzzle = function (grid, target) {
    function DFS(i, j, len) {
        if (len == target.length) return true

        if (i >= grid.length || i < 0 || j < 0 || j >= grid[0].length || target[len] !== grid[i][j]) {
            return false
        }

        let current = grid[i][j]
        grid[i][j] = '-1'

        let found = DFS(i + 1, j, len + 1) ||
                    DFS(i - 1, j, len + 1) ||
                    DFS(i, j + 1, len + 1) ||
                    DFS(i, j - 1, len + 1)
        grid[i][j] = current
        return found
    }


    for (let i = 0; i < grid.length; i++) {
        for (let j = 0; j < grid[0].length; j++) {
            if (grid[i][j] == target[0] && DFS(i, j, 0)) {
                return true
            }
        }
    }
    return false
};